<?php
/**
 * Created by PhpStorm.
 * User: cuipengyu
 * Date: 2018-12-29
 * Time: 14:35
 */
namespace app\admin\command;


use think\console\Command;
use think\Db;
use think\File;
use think\Log;
use tp5er\Backup;

class Loadfile extends Command
{
    protected  $files = [];

    protected function configure()
    {
        $this->setName('loadfile')->setDescription('Now loadfile to mip_file_upload ');
    }

    protected function execute(Input $input, Output $output){
        $this->loopFun(PUBLIC_PATH.'/uploads');

        foreach( $this->files as  $k => $f ){
            if(strpos('/.',$f) === 0){
                continue;
            }
            $rf = PUBLIC_PATH.$f;

            $mime = mime_content_type($rf);
            $fsize = filesize($rf);
            $output->writeln( $mime."::".$fsize);
            $fileExt = substr(strrchr($rf,'.'),1);

            $arr = ['uid'=>'','mime'=> $mime,'file_ext'=>$fileExt,'media_type'=>1, 'file_url' => $f,'file_save_path' =>$f,'file_size' => $fsize];

            $cnt = db('file_upload') -> where('file_save_path',$rf)->count();
            if($cnt == 0){
                db('file_upload') ->insert($arr);
            }

        }

    }

    private function loopFun( $dir ) {
        //取出文件或者文件夹
        $list = scandir( $dir );

        foreach( $list as $file ) {
            $location_dir = $dir . '/' . $file;

            if( is_file($location_dir) ){
                $relapath =  str_replace(PUBLIC_PATH,'',$location_dir );
                $this->files[] = $relapath;
            }

            //判断是否是文件夹 是就调用自身函数再去进行处理
            if( is_dir( $location_dir ) && '.' != $file && '..' != $file ) {
                $this->loopFun( $location_dir );
            }

        }
    }


}